import { Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param script javascript object
 * @param attributeName string
 * @param newValue string, object
 * @constructor
 */
class SetScriptValueCommand<T extends any,K extends keyof T> extends Command {
	object:Object3D;
	script:T;
	attributeName:K;
	oldValue:any;
	newValue:any;
	constructor( editor:Editor, object:Object3D, script:T, attributeName:K, newValue:any ) {

		super( editor );

		this.type = 'SetScriptValueCommand';
		this.name = `Set Script.${attributeName.toString()}`;
		this.updatable = true;

		this.object = object;
		this.script = script;

		this.attributeName = attributeName;
		this.oldValue = ( script !== undefined && script !== null ) ? script[ this.attributeName ] : undefined;
		this.newValue = newValue;

	}

	execute() {

		this.script[ this.attributeName ] = this.newValue;

		this.editor.signals.scriptChanged.dispatch();

	}

	undo() {

		this.script[ this.attributeName ] = this.oldValue;

		this.editor.signals.scriptChanged.dispatch();

	}

	update( cmd:SetScriptValueCommand<T,K> ) {

		this.newValue = cmd.newValue;

	}

}

export { SetScriptValueCommand };
